package com.glsc.ngateway.platform.service.glres;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscNetPortResource;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemDeploy;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.glres.GlscNetPortResourceRepository;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class GlscNetPortResourceService {

    @Resource
    private GlscNetPortResourceRepository netPortResourceRepository;

    @Resource
    private UserService userService;

    @Resource
    private GlscSystemDeployService glscSystemDeployService;

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<GlscNetPortResource> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<GlscNetPortResource> page = netPortResourceRepository.findAll(SpecificationUtil.buildSpecification(param, GlscNetPortResource.class), pageable);
        return page;
    }

    /**
     * 根据系统id查询关联的网络信息
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @param systemId
     * @return
     */
    public Page<GlscNetPortResource> pageBySystemId(int pageNo, int pageSize, Map<String, Object> param, Integer systemId, String sourceType) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        List<GlscSystemDeploy> deployList = glscSystemDeployService.listBySystemId(systemId, sourceType);
        if (CollectionUtils.isEmpty(deployList)) {
            return Page.empty();
        }

        Set<String> deployIpSet = deployList.stream().map(GlscSystemDeploy::getNodeIp).collect(Collectors.toSet());
        param.put("INSTR_address", StringUtils.join(deployIpSet, ","));


        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<GlscNetPortResource> page = netPortResourceRepository.findAll(SpecificationUtil.buildSpecification(param, GlscNetPortResource.class), pageable);
        return page;
    }


    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public GlscNetPortResource findById(Integer id) {
        Optional<GlscNetPortResource> optional = netPortResourceRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public GlscNetPortResource save(GlscNetPortResource obj) {
        return netPortResourceRepository.save(obj);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void delete(Integer id) {
        netPortResourceRepository.deleteById(id);
    }
}
